package com.hnkywl.sts.service.xsgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.NbgsDao;
import com.hnkywl.sts.dao.xsgl.KcDao;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.xsgl.Kc;
import com.hnkywl.sts.service.sys.YgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 库存service类
 * User: Administrator
 * Date: 13-4-25
 * Time: 下午3:02
 * To change this template use File | Settings | File Templates.
 */
@Service
public class KcService {
    private KcDao kcDao;
    private NbgsDao nbgsDao;

    /**
     * 查询库存报价列表
     * @param bjgsId
     * @return
     */
    @Transactional
    public String findAllKc(Long bjgsId,Date bjrq) {
        Nbgs nbgs = nbgsDao.get(bjgsId);
//        if(nbgs.getSjjg() == null){
//            bjgsId = null;
//        }
        List<Map> kcList = kcDao.findKcList(bjgsId,bjrq);
        return new Gson().toJson(kcList);
    }

    //报价物资管理页面的列表
    public String findAllKcgl(Long bjgsId) {
        List<Map> kcList = kcDao.findKcgl(bjgsId);
        return new Gson().toJson(kcList);
    }

    /**
     * 生成库存
     */
    @Transactional
    public void scKc(Long bjgsId){
        String deleteSql = "delete from t_biz_kc where bjgs_id = ? and fromht = ?";
        kcDao.createSQLQuery(deleteSql,bjgsId,1).executeUpdate();
        String sql = "insert into t_biz_kc(id,date_created,deleted,last_updated,createuser_id,bjgs_id,cd_sjzdz_id, gg_id,fromht) " +
                "  select seq_biz_kc.nextval,sysdate,0,sysdate,?,?,t.cdid,t.ggid,1 from ( " +
                "  select distinct gg.id ggid,nvl(sjzdz.id, cd2.id) cdid " +
                "    from t_sys_gg gg left join t_biz_kh kh on kh.jtwz_id = gg.id " +
                "    left join t_sys_sjzdz sjzdz on kh.cd_sjzdz_id = sjzdz.id " +
                "    left join t_biz_htmx mx on mx.wz_id = gg.id " +
                "    left join t_biz_ht ht on ht.id = mx.ht_id " +
                "    left join t_sys_sjzdz cd2 on mx.sjzdz_cd_id = cd2.id " +
                "   where gg.sfbj = 1 and (ht.sbgs_id = ? or kh.sbgs_id = ?)) t";
        kcDao.createSQLQuery(sql,YgService.getCurrentYg().getId(),bjgsId,bjgsId,bjgsId).executeUpdate();
    }

    /**
     * 批量保存库存
     * @param kcList
     */
    @Transactional
    public void saveKc(List<Kc> kcList){
        for(Kc kc : kcList){
            if(kc.getId() != null){
                updateKc(kc);
            } else {
                addKc(kc);
            }
        }
    }

    /**
     * 删除库存信息
     * @param kc
     */
    @Transactional
    public void deleteKc(Kc kc){
        Kc k = kcDao.get(kc.getId());
        k.setDeleted(1);
        k.setLastUpdated(new Date());
        kcDao.save(k);
    }

    public void addKc(Kc kc) {
        if(kc.getCd() != null && kc.getCd().getId() == null)
            kc.setCd(null);
        kc.setFromHt(0);
        kc.setCreateUser(YgService.getCurrentYg());
        kc.setCreateOrgan(YgService.getCurrentYg().getZggs());
        kc.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        kc.setDateCreated(new Date());
        kc.setLastUpdated(kc.getDateCreated());
        kcDao.save(kc);
    }

    public void updateKc(Kc kc) {
        if(kc.getCd() != null && kc.getCd().getId() == null)
            kc.setCd(null);
        Kc k = kcDao.get(kc.getId());
        k.setCd(kc.getCd());
        k.setLastUpdated(new Date());
        kcDao.save(k);
    }

    @Autowired
    public void setKcDao(KcDao kcDao) {
        this.kcDao = kcDao;
    }

    @Autowired
    public void setNbgsDao(NbgsDao nbgsDao) {
        this.nbgsDao = nbgsDao;
    }
}
